package com.zb.store.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.SelectVo;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjStorePositionService;
import com.zb.service.ZjStoreService;
import com.zb.store.service.ConStoreService;
import com.zb.utils.TokenUtil;
import com.zb.vo.StoreSelectVo;
import com.zb.vo.StoreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 仓库表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-10-30
 */
@Api(tags = "仓库类接口")
@RestController
@RequestMapping("/store")
public class ZjStoreController {
    @Resource
    private ConStoreService conStoreService;
    @Resource
    private ZjStoreService storeService;
    @Resource
    private ZjStorePositionService storePositionService;

    @SystemLog(moduleType = OperationLogModuleType.STORE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "仓库管理-仓库列表 - 新增或修改仓库")
    @ApiOperation("新增或修改仓库")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid StoreVo storeVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        storeService.save(custAccId, storeVo);
        return Result.success(null == storeVo.getId()? "新增仓库成功" : "修改仓库成功");
    }

    @ApiOperation("获取仓库列表本月的入/出库的数量")
    @GetMapping("/getListCurrentMothInWidthOutStoreQty")
    public Result getListCurrentMothInWidthOutStoreQty(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success().data(storeService.getListCurrentMothInWidthOutStoreQty(custAccId));
    }

    @ApiOperation("获取仓库列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody StoreSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取仓库列表成功").data(conStoreService.getList(custAccId, selectVo));
    }

    @ApiOperation("根据仓库ID获取仓库详情")
    @GetMapping("/getDetail/{id}")
    public Result getDetail(@PathVariable @ApiParam(value = "仓库ID", required = true) Long id) {
        return Result.success("获取仓库详情成功").data("row", storeService.getDetail(id));
    }

    @ApiOperation("根据仓库ID获取仓库库存明细")
    @PostMapping("/getStoreDetailGoodsList/{id}")
    public Result getStoreDetailGoodsList(@PathVariable @ApiParam(value = "仓库ID", required = true) Long id, @RequestBody PageUtil pageUtil) {
        return Result.success().data(conStoreService.getStoreDetailGoodsList(id, pageUtil));
    }

    @ApiOperation("根据仓库ID获取仓位列表")
    @PostMapping("/getStorePositionList/{storeId}")
    public Result getStorePositionList(@PathVariable @ApiParam(value = "仓库ID", required = true) Long storeId, @RequestBody SelectVo selectVo) {
        return Result.success("获取仓位列表成功").data(storePositionService.getStorePositionList(storeId, selectVo));
    }

    @ApiOperation("根据仓位ID获取仓位库存明细")
    @PostMapping("/getStorePositionDetailGoodsList/{storePositionId}")
    public Result getStorePositionDetailGoodsList(@PathVariable @ApiParam(value = "仓位ID", required = true) Long storePositionId, @RequestBody PageUtil pageUtil) {
        return Result.success().data(conStoreService.getStorePositionDetailGoodsList(storePositionId, pageUtil));
    }

    @SystemLog(moduleType = OperationLogModuleType.STORE_TYPE, type = OperationLogType.UPDATE, remark = "仓库管理-仓库列表 - 批量启用或禁用仓库")
    @ApiOperation("批量启用或禁用仓库")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "仓库状态：false-启用，true-禁用", required = true, example = "false") boolean status,
                          @RequestBody @ApiParam(value = "仓库ID", required = true) List<Long> ids) {
        conStoreService.updateDisabledBatchById(ids, status);
        return Result.success(status? "禁用仓库成功" : "启用仓库成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.STORE_TYPE, type = OperationLogType.DELETE, remark = "仓库管理-仓库列表 - 批量删除仓库")
    @ApiOperation("批量删除仓库")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "仓库ID", required = true) List<Long> ids) {
        conStoreService.deleteBatchById(ids);
        return Result.success("删除仓库成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.STORE_TYPE, type = OperationLogType.UPDATE, remark = "仓库管理-仓库列表 - 批量启用或禁用仓库")
    @ApiOperation("批量修改允许或不允许负库存")
    @PutMapping("/canNegativeStock")
    public Result canNegativeStock(@RequestParam @ApiParam(value = "是否允许负库存：false-不允许，true-允许", required = true, example = "false") boolean canNegativeStock,
                                   @RequestBody @ApiParam(value = "仓库ID", required = true) List<Long> ids) {
        storeService.updateCanNegativeStockBatchById(ids, canNegativeStock);
        return Result.success(canNegativeStock? "允许负库存成功" : "禁止负库存成功");
    }
}
